/**
 * 用户相关rest api
 */
import request from '@/utils/request'

//API常量
const APIS = {
    V1: "/v1"
}

function queryUserByPaging(page, size, conditions) {
    return request({
        url: APIS.V1 + `/system/user/list/${page}/${size}`,
        method: 'GET',
        params: conditions
    })
}

function loadUserRoles(page, size, userId, condition) {
    return request({
        url: APIS.V1 + `/system/user/${userId}/roles/${page}/${size}`,
        method: 'GET',
        params: condition
    })
}

function deleteUserById(id) {
    return request({
        url: APIS.V1 + `/system/user/${id}`,
        method: 'DELETE',
    })
}

function saveOrUpdateUser(user) {
    console.log("提交用户信息" + user)
    return request({
        url: APIS.V1 + "/system/user",
        method: 'POST',
        data: user
    })
}

function saveOrUpdateUserRole(userId, role) {
    console.log("提交用户角色信息" + userId)
    return request({
        url: APIS.V1 + `/system/user/role/${userId}`,
        method: 'POST',
        data: role
    })
}

function listAllAvailableRoles() {
    return request({
        url: APIS.V1 + "/system/role/list/all",
        method: 'GET',
    })
}

function addUserRole(userId, role) {
    return request({
        url: APIS.V1 + `/system/user/role/${userId}`,
        method: 'POST',
        data: role
    })
}


function delUserRole(userId, roleId) {
    return request({
        url: APIS.V1 + `/system/user/role/${userId}/${roleId}`,
        method: 'DELETE',
    })
}

// 角色列表
export function listByPage(param) {
    // alert(JSON.stringify(param));
    return request({
        url: APIS.V1 + `/system/user/listByPage`,
        method: 'get',
        params: param
    })
}


export function insertUser(data) {

    return request({
        url: APIS.V1 + `/system/user/insert`,
        method: 'POST',
        data
    })
}

export function updateUser(data) {
    return request({
        url: APIS.V1 + `/system/user/updateUser`,
        method: 'POST',
        data
    })
}

export function queryDictByName(name) {
    return request({
        url: APIS.V1 + `/system/dict/queryDictByName?name=` + name,
        method: 'get'
    })
}

export function getRoleAndDeptByUserId(userId) {
    return request({
        url: APIS.V1 + `/system/user/getRoleAndDeptByUserId?userId=` + userId,
        method: 'get'
    })
}

export function resetPwd(data) {
    return request({
        url: APIS.V1 + `/system/user/reset`,
        method: 'post',
        data
    })
}
export function importUser(data) {
    return request({
      url: APIS.V1+`/system/user/import`,
      method: 'post',
      data
    })
  }
export default {
    queryUserByPaging,
    deleteUserById,
    saveOrUpdateUser,
    saveOrUpdateUserRole,
    loadUserRoles,
    listAllAvailableRoles,
    addUserRole,
    delUserRole
}

//